package com.sujing.autoUpdate.sqlOption;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;

import com.sujing.autoUpdate.Model.UploadFile;

public class InsertFile {

	public String InsertFile(UploadFile up) {
		String Driver="com.mysql.jdbc.Driver";
		String url="jdbc:mysql://127.0.0.1:3306/uploadfile";
		String user="root";
		String password="123456";
		
		String fileName=up.getName();
		float fileVersion=up.getVersion();
		String filePath=up.getPath();
		Timestamp fileDate=up.getDate();
		
		try {
			Class.forName(Driver);
			try {
				Connection conn=DriverManager.getConnection(url,user,password);
				if(!conn.isClosed())
					System.out.println("Success database!!");
				String sql="insert into file(fileName,fileVersion,filePath,fileDate) values(?,?,?,?)";
				PreparedStatement pstmt=conn.prepareStatement(sql);
				pstmt.setString(1, fileName);
				pstmt.setFloat(2, fileVersion);
				pstmt.setString(3, filePath);
				pstmt.setTimestamp(4, fileDate);
				
				int rows=pstmt.executeUpdate();
				System.out.println(".............rows=.........."+rows);
				
				pstmt.close();
				conn.close();
				return "success";
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "fail";
		
		
	}

}
